/**
 * 
 */
package com.eis.sap.in.connector;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.TimeZone;

import com.eis.sap.in.connector.bean.FunctionImportingParameter;
import com.eis.sap.in.connector.conf.SapConnParameter;
import com.eis.sap.in.connector.parameter.SapParameterDefine;
import com.eis.thirdsys.connector.ThirdSysConnector;
import com.eis.thirdsys.connector.conf.ConnectorConfig;
import com.eis.thirdsys.connector.parameter.ParameterValue;
import com.sap.mw.jco.JCO;

/**
 * @author sh-panwei01
 *
 */
public class SapConnector extends ConnectorBase implements ThirdSysConnector {
	protected JCO.Client mConnection = null;
	protected JCO.Repository mRepository;

	/* (non-Javadoc)
	 * @see com.eis.function.connector.FunctionConnector#init(com.eis.function.connector.conf.ConnectorConfig, com.eis.function.connector.datatype.DataTypeList)
	 */
	public boolean init(ConnectorConfig connectorConfig) {
		super.connect(connectorConfig);
		return true;
	}

	/* (non-Javadoc)
	 * @see com.eis.function.connector.FunctionConnector#getFunctionSchema(java.lang.String, java.lang.String)
	 */
	public ArrayList<HashMap<String, Object>> getFunctionSchema(
			String functionId, String dataFetchId, List<ParameterValue> callParameters) {
		FunctionImportingParameter getSchemaPra = new FunctionImportingParameter();
		getSchemaPra.setParameterName("ID_FUNCTIONID");
		getSchemaPra.setList(false);
		getSchemaPra.setSingleParameterValue(functionId);
		List<FunctionImportingParameter> getSchemaPraList = new ArrayList<FunctionImportingParameter>();
		getSchemaPraList.add(getSchemaPra);
		

		return execute("/EISCO/GET_FUNCTIONID_SCHEMA", getSchemaPraList, "000000", "19970101");
	}

	/* (non-Javadoc)
	 * @see com.eis.function.connector.FunctionConnector#getFunctionData(java.lang.String, java.lang.String, java.lang.String, java.util.List)
	 */
	public ArrayList<HashMap<String, Object>> getFunctionData(
			String functionId, String dataFetchId, Date lastFetchDate,
			List<ParameterValue> callParameters) {
		List<FunctionImportingParameter> parameterList = convertParameterDataType(callParameters);

		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd HHmmss");
		String timeZone 
			= connectorConfig.getOneConf(SapConnParameter.timeZone.name()).getValue();
		formatter.setTimeZone(TimeZone.getTimeZone(timeZone));

		String formaterDate = formatter.format(lastFetchDate);
		String[] datePara = formaterDate.split(" ");
		

	    return execute(dataFetchId, parameterList, datePara[1], datePara[0]);
	}
	
	private List<FunctionImportingParameter> convertParameterDataType(List<ParameterValue> callParameters) {
		List<FunctionImportingParameter> returned = new ArrayList<FunctionImportingParameter>();
		
		for(ParameterValue onePara : callParameters) {
			FunctionImportingParameter converted = new FunctionImportingParameter();
			returned.add(converted);
			converted.setParameterName(onePara.getElementValue(SapParameterDefine.parameterName.name()).getValue()); 
			if ("List".equalsIgnoreCase(onePara.getElementValue(SapParameterDefine.parameterType.name()).getValue())) {
				converted.setList(true);
				if (onePara.getParameterValue() != null) {
					converted.setListParameterValue(onePara.getElementValue(SapParameterDefine.parameterValue.name()).getValue().split(","));
				}
			} else {
				converted.setList(false);
				converted.setSingleParameterValue(onePara.getElementValue(SapParameterDefine.parameterValue.name()).getValue());
			}
			
		}
		return returned;
	}
}
